package com.jpyl.jpMedicalTreatment.view.activity.self;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import com.forxindu.kit.box.ViewKit;
import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.model.ResponseModel;
import com.jpyl.jpMedicalTreatment.model.RxSpf_UserModel;
import com.jpyl.jpMedicalTreatment.network.Network;
import com.jpyl.jpMedicalTreatment.network.api.ApiError;
import com.jpyl.jpMedicalTreatment.network.api.RequestApi;
import com.jpyl.jpMedicalTreatment.util.StringUtil;
import com.jpyl.jpMedicalTreatment.view.activity.BaseActivity;
import com.jpyl.jpMedicalTreatment.view.activity.LoginActivity;
import com.jpyl.jpMedicalTreatment.view.activity.TestActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 安全管理（修改密码）
 * Created by 15697 on 2019/4/1.
 */

public class UpdatePasswordActivity extends BaseActivity {

    @BindView(R.id.et_updatePwd_old)
    EditText etUpdatePwdOld;
    @BindView(R.id.et_updatePwd_new)
    EditText etUpdatePwdNew;
    @BindView(R.id.et_updatePwd_newSecond)
    EditText etUpdatePwdNewSecond;
    final RequestApi publicApi = Network.getRequestApi();
    RxSpf_UserModel spfUserModel;
    String loginName = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_update_password;
    }

    @Override
    public void afterCreate(Bundle savedInstanceState) {
        mTitleTv.setText("修改密码");
        spfUserModel = RxSpf_UserModel.create(this);
        loginName = spfUserModel.loginname().get();
    }


    @OnClick(R.id.btn_updatePwd)
    public void onViewClicked() {
        String oldPwd = etUpdatePwdOld.getText().toString();
        String newPwd = etUpdatePwdNew.getText().toString();
        String secondPwd = etUpdatePwdNewSecond.getText().toString();
        if (StringUtil.isEmpty(oldPwd)) {
            etUpdatePwdOld.requestFocus();
            ViewKit.showToast(UpdatePasswordActivity.this, "请输入原密码");
            return;
        }
        if (StringUtil.isEmpty(newPwd)) {
            etUpdatePwdNew.requestFocus();
            ViewKit.showToast(UpdatePasswordActivity.this, "请输入新密码");
            return;
        }
        if (StringUtil.isEmpty(secondPwd)) {
            etUpdatePwdNewSecond.requestFocus();
            ViewKit.showToast(UpdatePasswordActivity.this, "请重新输入新密码");
            return;
        }
        if (!newPwd.equals(secondPwd)) {
            ViewKit.showToast(UpdatePasswordActivity.this, "两次输入的密码不一致，请重新输入");
            return;
        }

        ChangePassword(oldPwd, newPwd);
    }

    /**
     * 修改密码
     */
    void ChangePassword(String oldPwd, String newPwd) {
        publicApi.jpylChangePassword(loginName, oldPwd, newPwd).map(new Func1<ResponseModel<String>,
                String>() {
            @Override
            public String call(ResponseModel<String> responseModel) {
                if (responseModel.getCode() != 0) {
                    String msg = responseModel.getMsg();
                    throw new ApiError(responseModel.getCode(), msg);
                }
                return responseModel.getMsg();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (e instanceof ApiError) {
                            ApiError error = (ApiError) e;
                            ViewKit.showToast(UpdatePasswordActivity.this, error.getMsg() + "");
                        }
                    }

                    @Override
                    public void onNext(String msg) {
                        ViewKit.showToast(UpdatePasswordActivity.this, msg + "");
                        finish();
                        startActivity(new Intent(getApplicationContext(), LoginActivity.class));
                    }
                });
    }
}
